﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mortgage.DAL.Entity;
using Mortgage.DAL;

namespace Mortgage.Srv
{

    public interface IInterestRateSrv
    {
        List<vwInterestRate> GetAllInterestInfo();
        List<vwInterestRate> GetAllInterestInfo(int agentId);

        List<tInterestRate> GetAll();
        List<tzConfig> GetConfigIntestName();
        tInterestRate GetById(int id);
        int Insert(tInterestRate insObj);
        int Update(tInterestRate newsObj);
        int Delete(tInterestRate delObje);
        List<vwInterestRate> Find(int? codeId, DateTime? dteUse, int? agentId);

    }

    class InterestRateSrv : IInterestRateSrv
    {
        const string INTEREST_CODE = "INTER_TABLE";
        private MortgageDataContext dataContext;

        public InterestRateSrv()
        {
            dataContext = new MortgageDataContext();

        }

        public List<tzConfig> GetConfigIntestName()
        {
            List<tzConfig> result;
            result = dataContext.tzConfigs.Where(c => c.Code == INTEREST_CODE && c.IsDelete == false).ToList();
            return result;
        }


        public List<tInterestRate> GetAll()
        {
            List<tInterestRate> result;
            result = dataContext.tInterestRates.ToList();
            return result;
        }

        public tInterestRate GetById(int id)
        {
            tInterestRate result = dataContext.tInterestRates.Where(a => a.ID == id).FirstOrDefault();
            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="insObj"></param>
        /// <returns>
        /// 0: success
        /// other: error code
        /// </returns>
        public int Insert(tInterestRate insObj)
        {

            if (dataContext.tInterestRates.Any(m => !m.IsDelete && m.BeginDate == insObj.BeginDate && m.Age_ID == insObj.Age_ID && m.CodeId == insObj.CodeId))
            {
                return Update(insObj);
            }

            tInterestRate preInterCase = dataContext.tInterestRates.Where(m => !m.IsDelete && m.BeginDate > insObj.BeginDate && m.Age_ID == insObj.Age_ID && m.CodeId == insObj.CodeId).FirstOrDefault();

            tInterestRate midInterCase = dataContext.tInterestRates.Where(m => !m.IsDelete && m.BeginDate < insObj.BeginDate && m.EndDate.HasValue && m.Age_ID == insObj.Age_ID && m.CodeId == insObj.CodeId).FirstOrDefault();

            if (preInterCase != null)
            {
                insObj.EndDate = preInterCase.BeginDate.AddDays(-1);
            }
            else if (midInterCase != null)
            {
                midInterCase.EndDate = insObj.BeginDate.AddDays(-1);
            }
            else
            {

                tInterestRate forInterCase = dataContext.tInterestRates.Where(m => !m.IsDelete && m.BeginDate < insObj.BeginDate && m.Age_ID == insObj.Age_ID && m.CodeId == insObj.CodeId).OrderByDescending(a => a.BeginDate).FirstOrDefault();
                if (forInterCase != null)
                {
                    forInterCase.EndDate = insObj.BeginDate.AddDays(-1);
                }
                insObj.EndDate = null;

            }
            dataContext.tInterestRates.InsertOnSubmit(insObj);
            dataContext.SubmitChanges();
            return 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="newsObj"></param>
        /// <returns>
        /// 0: success
        /// 1: No find update object
        /// 2: Begin date not valid
        /// other: error code
        /// </returns>
        public int Update(tInterestRate newsObj)
        {
            //Begin date can not update
            tInterestRate entity = dataContext.tInterestRates.Where(a => a.ID == newsObj.ID).FirstOrDefault();
            if (entity == null) return 1;
            int dtScale = entity.BeginDate.CompareTo(newsObj.BeginDate);

            // Passs date

            entity.ModifiedBy = newsObj.ModifiedBy;
            entity.ModifiedDate = newsObj.ModifiedDate;

            entity.CodeId = newsObj.CodeId;
            entity.InterestRate = newsObj.InterestRate;
            entity.NumOfDayExpire = newsObj.NumOfDayExpire;
            entity.BeginDate = newsObj.BeginDate;
            entity.Description = newsObj.Description;

            dataContext.SubmitChanges();
            return 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="delObje"></param>
        /// <returns>
        /// 0: succcessful
        /// 1: No find delete object
        /// other: error code
        /// </returns>
        public int Delete(tInterestRate delObje)
        {
            tInterestRate entity = dataContext.tInterestRates.Where(a => a.ID == delObje.ID).FirstOrDefault();
            if (entity == null) return 1;

            entity.IsDelete = true;
            entity.ModifiedBy = delObje.ModifiedBy;
            entity.ModifiedDate = delObje.ModifiedDate;

            //dataContext.tInterestRates.DeleteOnSubmit(entity);
            dataContext.SubmitChanges();

            return 0;
        }


        public List<vwInterestRate> Find(int? codeId, DateTime? dteUse, int? agentId)
        {

            List<vwInterestRate> result = (from i in dataContext.vwInterestRates
                                           where (codeId == null || i.CodeId == codeId.GetValueOrDefault())
                                           && (dteUse == null || (i.BeginDate <= dteUse.GetValueOrDefault() && i.EndDate.GetValueOrDefault(DateTime.MaxValue) >= dteUse.GetValueOrDefault()))
                                           && (agentId == null || i.Age_ID == agentId.GetValueOrDefault())
                                           && (i.IsDelete == false)
                                           select i).ToList();
            return result;
        }



        public List<vwInterestRate> GetAllInterestInfo()
        {
            return dataContext.vwInterestRates.Where(i => i.IsDelete == false).OrderBy(m => m.Age_ID).ThenBy( m => m.CodeId).ThenBy(m=> m.BeginDate).ToList();
        }



        public List<vwInterestRate> GetAllInterestInfo(int agentId)
        {
            return dataContext.vwInterestRates.Where(i => i.IsDelete == false && agentId == i.Age_ID).OrderBy(m => m.Age_ID).ThenBy(m => m.CodeId).ThenBy(m => m.BeginDate).ToList();
        }

    }
}


